package com.service.device;

import com.common.ErrorCodeEnum;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;

import com.dao.secondary.device.DeviceObdBatchRepairRecordInfoMapper;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.pojo.device.DeviceObdBatchRepairRecordInfo;
import com.model.pojo.device.DeviceObdBatchRepairRecordMappingInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



/**
 * @Author: zql
 * @Description: OBD设备工厂批量维修记录服务
 * @Data: 2024-04-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceObdBatchRepairRecordInfoService {
    private final DeviceObdBatchRepairRecordInfoMapper deviceObdBatchRepairRecordInfoMapper;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    private final DeviceObdBatchRepairRecordMappingInfoService deviceObdBatchRepairRecordMappingInfoService;
    public int save(DeviceObdBatchRepairRecordInfo info, List<DeviceObdBaseInfo> baseList,List<String> sdtuList){
        int num = 0;
        info.setCreateTime(new Date());
        deviceObdBatchRepairRecordInfoMapper.insertSelective(info);
        if(info.getRecordId()>0){
            List<DeviceObdBaseInfo> tempList = new ArrayList<>();
            List<DeviceObdBatchRepairRecordMappingInfo> recordMappingList = new ArrayList<>();
            for(int i=0;i<baseList.size();i++){
                DeviceObdBaseInfo baseInfo = baseList.get(i);
                baseInfo.setLon("");
                baseInfo.setLat("");
                baseInfo.setImei("");
                baseInfo.setSim("");
                baseInfo.setWjdy("");
                baseInfo.setGgyxh("");
                baseInfo.setCjbs("");
                baseInfo.setCjzt(0);
                baseInfo.setModuleType("");
                baseInfo.setEepromDeleteCount("");
                baseInfo.setHasPass2(0);
                baseInfo.setCheckoutTime2(null);
                baseInfo.setLastModifiedTime(new Date());
                tempList.add(baseInfo);
                DeviceObdBatchRepairRecordMappingInfo mappingInfo = new DeviceObdBatchRepairRecordMappingInfo();
                mappingInfo.setBatchId(info.getBatchId());
                mappingInfo.setRecordId(info.getRecordId());
                mappingInfo.setDtu(baseInfo.getDtu());
                mappingInfo.setSdtu(sdtuList.get(i));
                mappingInfo.setCreateTime(new Date());
                recordMappingList.add(mappingInfo);
            }
            deviceObdBaseInfoService.updateList(tempList);
            deviceObdBatchRepairRecordMappingInfoService.saveList(recordMappingList);
        }
        return num;
    }
}
